/*
* Copyright 2008 Google Inc. 
* Licensed under the Apache License, Version 2.0:
*  http://www.apache.org/licenses/LICENSE-2.0
*/
package {

import com.google.maps.LatLng;
import com.google.maps.Map;

import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import flash.net.*;

import mx.containers.Box;
import mx.controls.Image;
import mx.controls.LinkButton;
import mx.controls.SWFLoader;
import mx.controls.Text;
import mx.core.UIComponent;

public class InfoWindowYoutube extends UIComponent {
  public var zoomedIn:Boolean = false;
  public var map:Map;
  public var location:Object;
  public var outBox:LocalConnection;
  public var swfLoader:SWFLoader;
  
  public function InfoWindowYoutube(map:Map, location:Object, outBox:LocalConnection, swfLoader:SWFLoader) {
    this.map = map;
    this.location = location;
    this.outBox = outBox;
    this.swfLoader = swfLoader;
    trace("XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    trace(this.swfLoader);
  }
  
  public function populate():void {
   var clickUrl:String = "";
   var embedUrl:String = "";
   var width:Number;
   var height:Number;
    var box:Box = new Box();
    box.setStyle("horizontalAlign", "center");
    box.width = 305;
    box.height = 285;
    
    
    var bodyTextField:Text = new Text();
    bodyTextField.setStyle("textAlign", "center");
    bodyTextField.percentWidth = 90;
    bodyTextField.htmlText = "<center><b>" + location.full_location + "</b></center>";
    bodyTextField.selectable = false;
    box.addChild(bodyTextField);
    
    var clickMoreMessage:String;
    if (location.video != "") {
    	embedUrl = "http://www.youtube.com/v/RUgh371O_W8";
                clickUrl = "http://www.youtube.com";
                width = 480;
                height = 385;
        
        if (this.swfLoader.parent) {
              this.swfLoader.parent.removeChild(this.swfLoader);
            }
        box.addChild(this.swfLoader);
        
        clickMoreMessage = Messages.strings.youtubeMore[map.language];
    } else {
    	embedUrl = 'http://mw2.google.com/mw-panoramio/photos/small/' + location.panoramio_id + '.jpg';
                clickUrl = 'http://www.panoramio.com/photo/' + location.panoramio_id;
                width = 280;
                height = 300;
        var img:Image = new Image();
        img.source = embedUrl;
        img.width = 200;
        img.height = 180;
        box.addChild(img);
        clickMoreMessage = Messages.strings.panoramioMore[map.language];
    }
    
    var clickMore:LinkButton = new LinkButton();
    clickMore.percentWidth = 100;
    clickMore.label = clickMoreMessage;
    clickMore.styleName = "linkButtonStyle";
    clickMore.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
        ExternalInterface.call("trackPanoClick");
        var request:URLRequest = new URLRequest(clickUrl);
        navigateToURL(request, "_blank");
        // todo: catch errors?
    });
    box.addChild(clickMore);
    
    var latlng:LatLng = new LatLng(location.lat, location.lng);
    var defaultMaxZoom:Number = 13;
    if (location.zoom != "") { defaultMaxZoom = location.zoom; }
    var zoomIn:LinkButton = new LinkButton();
    zoomIn.percentWidth = 100;
    zoomIn.styleName = "linkButtonStyle";
    if (map.getZoom() < defaultMaxZoom) {
        zoomIn.label = clickMoreMessage = Messages.strings.zoomIn[map.language];
        zoomedIn = false;
    } else {
        zoomIn.label = clickMoreMessage = Messages.strings.zoomOut[map.language];
        zoomedIn = true;
    }
    zoomIn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
        ExternalInterface.call("trackZoomClick");
        if (!zoomedIn) {
           zoomedIn = true;
           map.setCenter(latlng, defaultMaxZoom);
           zoomIn.label = Messages.strings.zoomOut[map.language];
        } else {
            zoomedIn = false;
            map.setCenter(latlng, 2);
            zoomIn.label = Messages.strings.zoomIn[map.language];
        }
    });
    box.addChild(zoomIn);
    addChild(box);   

   }
}

}
